package com.ttbj.controller.sysuser;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ttbj.product.api.SalesmanFacade;
import com.ttbj.product.bean.dto.SalesmanDto;
import com.ttbj.product.bean.dto.ViewData;
import com.ttbj.util.ResultUtil;
import com.ttbj.util.SafetyUtils;
import com.ttbj.util.ViewModelUtil;
import com.zywl.common.utils.DateUtils;

import freemarker.template.utility.DateUtil;

@Controller
@RequestMapping("/salesman/user")
public class SalesmanController {
	private static final Logger logger = LoggerFactory.getLogger(SalesmanController.class);
	@Autowired
	SalesmanFacade salesmanFacade = null;

	/**
	 * 业务员登陆
	 * 
	 * @param name
	 *            用户名
	 * @param pwd
	 *            密码
	 * @throws Exception
	 */
	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	@ResponseBody
	public ViewData login(HttpServletRequest request, HttpServletResponse response, @RequestParam("name") String name, @RequestParam("pwd") String pwd, String registId) throws Exception {
		pwd = SafetyUtils.addSalt(pwd, name);
		if (StringUtils.isEmpty(pwd)) {
			SafetyUtils.error(response, ResultUtil.createFail("messages", 114), "密码不能为空");
		}
		SalesmanDto user = salesmanFacade.findSalesman(new SalesmanDto(name, pwd));
		if (null == user) {
			return ResultUtil.createFail("messages", 114);
		}
		String token = UUID.randomUUID().toString();
		user.newToken(token).updateDate(DateUtils.getLong2LStr(System.currentTimeMillis())).registId(registId);
		salesmanFacade.updateUser(user);
		return ViewModelUtil.buildSuccess("成功", token);
	}
	
	/**
	 * 登出接口
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/logout.do", method = RequestMethod.POST)
	@ResponseBody
	public ViewData logout(HttpServletRequest request, HttpServletResponse response,String token) throws Exception {
		SalesmanDto salesman = (SalesmanDto) request.getAttribute("user");
		salesman.setSalesmanToken(null);
		salesmanFacade.logout(salesman);
		return ViewModelUtil.buildSuccess("成功");
	}
	/**
	 * 注册
	 * @param request
	 * @param response
	 * @param name
	 * @param pwd
	 * @param registId
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	@RequestMapping(value = "/regist.do", method = RequestMethod.POST)
	@ResponseBody
	public ViewData regist(HttpServletRequest request, HttpServletResponse response, @RequestParam("name") String name, @RequestParam("pwd") String pwd, String registId) throws UnsupportedEncodingException, IOException {
		pwd = SafetyUtils.addSalt(pwd, name);
		if (StringUtils.isEmpty(pwd)) {
			SafetyUtils.error(response, ResultUtil.createFail("messages", 114), "密码不能为空");
		}
		SalesmanDto salesmanDto = new SalesmanDto();
		salesmanDto.setSalesmanName(name);
		SalesmanDto user = salesmanFacade.findSalesman(salesmanDto);
		if(null != user){
			return ResultUtil.createFail("messages", 208);
		}
		salesmanDto.setSalesmanPwd(pwd);
		salesmanFacade.addUser(salesmanDto);
		return ViewModelUtil.buildSuccess("成功");
	}
	
	/**
	 * 业务员信息查询
	 */
	@RequestMapping(value = "/info.do", method = RequestMethod.POST)
	@ResponseBody
	public ViewData userInfo(HttpServletRequest request, HttpServletResponse response, @RequestParam("token") String token) throws UnsupportedEncodingException, IOException {
		SalesmanDto salesman = (SalesmanDto) request.getAttribute("user");
		salesman.setSalesmanPwd(null);
		return ViewModelUtil.buildSuccess("成功",salesman);
	}
	
	 

}
